<?php

namespace App\Admin\Controllers\Invoicing;


use App\Admin\Actions\Grid\Row\SwtProcurement\CompleteReplenishment;
use App\Admin\Actions\Grid\Row\SwtProcurement\ReplenishmentDetail;
use App\Admin\Actions\Grid\Row\SwtProcurement\StartReplenishment;
use App\Admin\Renderable\EmployeeRenderable;
use App\Admin\Renderable\GoodsRenderable;
use App\Admin\Renderable\MachineRenderable;
use App\Admin\Repositories\SwtReplenishment;
use App\Models\SwtEmployee;
use App\Models\SwtGood;
use App\Models\SwtMachine;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Ramsey\Uuid\Uuid;

class SwtReplenishmentController extends AdminController
{
    protected $title = '补货出库';

    /**
     * 查看采购单列表
     * return
     */
    protected function grid()
    {
        return Grid::make(new SwtReplenishment(), function (Grid $grid) {

            $grid->column('id')->sortable();
            $grid->column('replenishment_no','补货单号');
            $grid->column('replenishment_status','补货单状态')->using(['未出库','出库','补货完成'])->label(['danger','info','success']);
            $grid->column('replenishment_machine_id','设备')->display(function ($value) {
                    $machine = SwtMachine::where('id',$value)->first();
                    return $machine ? $machine->machine_name : '';
            });

            $grid->column('operator_name','补货员');
            $grid->column('cost_prices','补货总成本')-> display( function ($value) {
                return '￥'.bcdiv($value, 1000, 2);
            });;
            $grid->column('remake','备注');

            $grid->column('replenishment_time','最晚补货时间');

            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('replenishment_no','采购单号');
                $filter->between('created_at');
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                //采购单详情按钮
                $actions -> prepend(new ReplenishmentDetail());

                if($actions->row->replenishment_status == 0) {
                    //开始出库 按钮
                    $actions -> prepend(new StartReplenishment());
                    $actions -> disableDelete(false);
                }

                if($actions->row->replenishment_status == 1) {
                    //补货完成按钮
                    $actions -> append(new CompleteReplenishment());
                }

            });

            $grid->quickSearch('replenishment_no');
            $grid->disableCreateButton(false);
            $grid->model()->orderBy('id', 'desc');

        });
    }



    /**
     * 查看采购单详情
     * return
     */
    protected function form()
    {
        return Form::make(new SwtReplenishment(), function (Form $form) {
            $form->selectTable('replenishment_machine_id', '选择设备')
                ->title('设备表')
                ->from(MachineRenderable::make())
                ->rules('required', ['required' => '商品名不能为空'])
                ->model(SwtMachine::class, 'id', 'machine_name')
                ->required();

            $form -> selectTable('operator_id','补货员')
                -> title('员工表')
                -> from(EmployeeRenderable::make(['type' => 2]))
                -> rules('required', [ 'required' => '用户不能为空'])
                -> model(SwtEmployee::class, 'id', 'name')
                -> required();

            $form->date('replenishment_time', '预计补货时间');
            $form->text('remake', '备注');

            $form->hidden('replenishment_no');
            $form->hidden('operator_name');
            $form->hidden('replenishment_status');
            $form->hidden('cost_prices');

            $form->hasMany('goods','添加补货商品',function ($form) {
                $form->row(function (Form\Row $form) {
                    $form->width(4)
                        ->selectTable('good_id', '商品名')
                        ->title('商品表')
                        ->from(GoodsRenderable::make())
                        ->rules('required', ['required' => '商品名不能为空'])
                        ->model(SwtGood::class, 'id', 'goods_name')
                        ->required();

                    $form->width(4)
                        ->text('replenishment_quantity', '预计补货数量')
                        ->type('number')
                        ->rules('required', ['required' => '数量不能为空'])
                        ->required();

                });
            })->useTable();



            $form->saving(function (Form $form) {
                $form->input('replenishment_no','BH'.strtoupper(MD5(Uuid::uuid4() -> toString())));
                $form->input('replenishment_status',0);
                $employee = SwtEmployee::where('id', $form->operator_id)->first();
                $form->input('operator_name',$employee->name ?? '');
                $cost_price = 0;
                foreach($form->goods as $key => $good){
                    $swt_good= SwtGood::where('id', $good['good_id'])->first();
                    $cost_price += $swt_good->cost_price * $good['replenishment_quantity'];
                }
                $form->input('cost_prices',$cost_price);
            });

        });
    }



}
